﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace chatblog
{
    public partial class blog : System.Web.UI.Page
    {
        string userid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                bind2();
                //bind3();
            }
        }
        /// <summary>
        /// 绑定分类
        /// </summary>
        private void bind1()
        {
            cbusercata ct = cbusercata.Find(" cblayer = 1 and cbuserid = '"+userid+"'");
            GridView_cata.DataSource = ct.MyTable;
            GridView_cata.DataBind();
        }
        /// <summary>
        /// 绑定正文列表
        /// </summary>
        private void bind2()
        {
            string sql = "";
            sql = " select top 100 * from vcblist where cblisttype = '发帖'  and cbuserid = '" + userid + "' ";

            if (this.Request.Params["cbusercataid"] != null)
            {
                sql += " and cbusercataid = '" + this.Request.Params["cbusercataid"].ToString() + "' ";
            } 
            sql += "  order by jccreatedate desc ,jccreatetime desc ";
            DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            WebPager_content.DataSource = dt;
            WebPager_content.DataBind();
        }
        /// <summary>
        /// 处理request
        /// </summary>
        private void dealrequest()
        {
            
            if (this.Request.Params["userid"] == null)
            {
                if (AliApi.appUserId != "")
                {
                    userid = AliApi.appUserId;
                }
                else
                {
                    this.Response.Redirect(ResolveUrl("~/blog/default.aspx"));
                }
                HyperLink_default.NavigateUrl = ResolveUrl("~/blog.aspx");
                cbuser u = cbuser.Find(" recid = " +AliApi.appDomainID);
                if (u.blogtitle.ToString()!="")
                {
                    HyperLink_default.Text = u.blogtitle.ToString();
                    Literal_desc.Text = u.blogdesc.ToString();
                }
                else
                {
                    HyperLink_default.Text = u.cbusernickname.ToString() + "的博客首页";
                }

                u.cbloginquantity.Value++; //博客查看数,增加字段后,要改过来,先挪用
                u.Modify();
                DataTable a1;
                string sql = "";
                sql = " select sum(isnull(chakanshu,0)) from cblist where cbstatus = '有效' "
                    + " and cbuserid = '" + u.cbuserid.ToString() + "' and cblisttype = '发帖'";
                //+ " and jccreatedate <= '" + DateTime.Today.AddDays(1).ToShortDateString()
                //+ "' and jccreatedate >= '" + DateTime.Today.ToShortDateString() + "' ";
                a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                int jsq = zfunction.str2int(a1.Rows[0].ItemArray[0].ToString()) + u.cbloginquantity.Value;
                Label_jsq.Text = jsq.ToString() ;
            }
            else
            { 
                cbuser u = cbuser.Find(" recid = "+this.Request.Params["userid"].ToString());
                userid = u.cbuserid.ToString();
                HyperLink_default.NavigateUrl = ResolveUrl("~/blog/") + this.Request.Params["userid"].ToString() + ".html";
                HyperLink_addfriend.NavigateUrl = ResolveUrl("~/bbs/addfriend.aspx?recid=") + u.RECID.ToString();
                if (u.blogtitle.ToString() != "")
                {
                    HyperLink_default.Text = u.blogtitle.ToString();
                    Literal_desc.Text = u.blogdesc.ToString();
                }
                else
                {
                    HyperLink_default.Text = u.cbusernickname.ToString()+"的博客首页";
                }

                u.cbloginquantity.Value++;//博客查看数,增加字段后,要改过来,先挪用
                u.Modify();
                DataTable a1;
                string sql = "";
                sql = " select sum(isnull(chakanshu,0)) from cblist where cbstatus = '有效' "
                    + " and cbuserid = '" + u.cbuserid.ToString() + "' and cblisttype = '发帖'";
                //+ " and jccreatedate <= '" + DateTime.Today.AddDays(1).ToShortDateString()
                //+ "' and jccreatedate >= '" + DateTime.Today.ToShortDateString() + "' ";
                a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                int jsq =zfunction.str2int( a1.Rows[0].ItemArray[0].ToString()) + u.cbloginquantity.Value;
                Label_jsq.Text = jsq.ToString();
            }
            this.Title = HyperLink_default.Text;
        }
        static public string getstr()
        {
            if (AliApi.appDomainID != "" && HttpContext.Current.Request.Params["userid"] == null)
            {
                System.Web.UI.Control c = new System.Web.UI.Control();
                HttpContext.Current.Response.Redirect(c.ResolveUrl("~/blog/") + AliApi.appDomainID + ".html");
            }
            if (HttpContext.Current.Request.Params["userid"] == null)
            {
                return "";
            }
            return "&userid=" + HttpContext.Current.Request.Params["userid"].ToString();
        }

        protected void WebPager_content_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            bind2();
            this.WebPager_content.CurrentPageIndex = e.NewPageIndex;
        }

        protected void Button_search_Click(object sender, EventArgs e)
        {
            this.Response.Redirect(ResolveUrl("~/blog/blogsearch.aspx?userid=" )
                + this.Request.Params["userid"].ToString()
                + "&keyword=" + TextBox_keyword.Text
                );
        }

        
    }
}
